// 规则：
// 1. 如果city为null或以"33"开头，返回默认结果 [instCode: "TEST", instName: "默认机构", storeId: "DEFAULT_STORE"]
// 2. 输入的city（如"440500"）必须在configData.city（List<String>）中
// 3. 返回第一个匹配所有条件的[instCode, instName, storeId]，若无匹配返回null
import org.slf4j.LoggerFactory

try {
  def logger = LoggerFactory.getLogger('LiteFlowScript')
  logger.info('开始执行城市匹配输出结果')

  def requestData = _meta.requestData
  def city = requestData.get('city')
  def filteredConfigs = defaultContext.getData('filteredConfigs')

  logger.info('开始执行城市匹配，输入城市为：{}', city)
  logger.info('过滤后的配置数量：{}，数量详情为：{}', filteredConfigs?.size() ?: 0, filteredConfigs.toString())

  if (city == null || city.startsWith('33')) {
    logger.info('CityMatchNode: returning default result for city={}', city)
    defaultContext.setData('result', [instCode: 'TEST', instName: '默认机构', storeId: 'DEFAULT_STORE'])
    return
  }

  if (!filteredConfigs || filteredConfigs.isEmpty()) {
    logger.info('CityMatchNode: filteredConfigs is null or empty, returning default')
    defaultContext.setData('result', [instCode: 'TEST', instName: '默认机构', storeId: 'DEFAULT_STORE'])
    return
  }

  for (item in filteredConfigs) {
    def configData = item.config.configData
    def cityList = configData.get('city') 
    logger.info('CityMatchNode: checking:{}', item.toString(), cityList)
    if (cityList && city in cityList) {
      defaultContext.setData('result', [instCode: item.instCode, instName: item.instName, storeId: item.storeId])
      logger.info('城市匹配结果：{}', item.toString())
      return 
    }
  }

  logger.info('CityMatchNode: no match found, returning default')
  defaultContext.setData('result', [instCode: 'TEST', instName: '默认机构', storeId: 'DEFAULT_STORE'])
} catch (Exception e) {
  logger.error('CityMatchNode: error occurred, message={}', e.message)
  defaultContext.setData('result', [instCode: 'TEST', instName: '默认机构', storeId: 'DEFAULT_STORE'])
}
